package com.victor.controller.app;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.victor.entity.ticketorder.TicketOrders;
import com.victor.service.TicketOrderService;
import com.victor.utils.ClassFilter;
import com.victor.utils.Msg;
/**
 * 票务
 * @author 
 *
 */
@RestController
@RequestMapping("/appticketorder")
public class TicketorderController {
	@Autowired
	private TicketOrderService service;
	
	/**
	 * 票务下单
	 * @param ticketOrders
	 * @return
	 */
	@RequestMapping("/addticket")
	public Msg addTicketOrder(@RequestBody TicketOrders ticketOrders) {
		String[] str = {"uid","flag","jqlistid","ordername","jqticketid","orderprice","uname","ordernumber","contentphone","photo"};
		boolean empty = ClassFilter.somePropertyIsEmpty(ticketOrders, str);
		if (empty ==true) {
			return Msg.fail("入参失败");
		}
		if (ticketOrders.getFlag().equals("0")) {
			if (ticketOrders.getUsetime() == null || ticketOrders.getUsetime().isEmpty()) {
				return Msg.fail("景区入参失败");
			}
		}
		boolean insertTicketorder = service.insertTicketorder(ticketOrders);
		if (!insertTicketorder) {
			return Msg.fail("添加购片信息失败");
		}
		
		if (ticketOrders.getTicketorderUsers() != null && ticketOrders.getTicketorderUsers().size()>0) {
			for (int i = 0; i < ticketOrders.getTicketorderUsers().size(); i++) {
				ticketOrders.getTicketorderUsers().get(i).setToid(String.valueOf(ticketOrders.getId()));
				boolean insertTicketorderUser = service.insertTicketorderUser(ticketOrders.getTicketorderUsers().get(i));
				if (!insertTicketorderUser) {
					return Msg.fail("添加出行人失败");
				}
			}
		}
		return Msg.success();
	}
	/**
	 * 订单状态
	 * @param useflag
	 * @param uid
	 * @param row
	 * @param page
	 * @return
	 */
	@RequestMapping("/allorder")
	public Msg findOrders(String useflag,String uid,int row,int page) {
		if (uid == null || uid.isEmpty() || useflag == null || useflag.isEmpty()) {
			return Msg.fail("入参失败");
		}
		PageInfo<TicketOrders> selOrderByUidAndUseflag = service.selOrderByUidAndUseflag(useflag,uid,page,row);
		return Msg.success().add("list", selOrderByUidAndUseflag);
	}

	/**
	 * 订单详情
	 * @param id
	 * @return
	 */
	@RequestMapping("/orderdetails")
	public Msg findTicketOrderDetails(String id) {
		if ( id == null || id.isEmpty()) {
			return Msg.fail("入参错误");
		}
		TicketOrders selAppById = service.selAppById(id);
		return Msg.success().add("info", selAppById);
	}
}
